package com.neuedu.my12306.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringContextUtil {
	private static final String CONFIG = "application.xml";

	public static ApplicationContext getApplicationContext() {
		ApplicationContext context = null;

		try {
			context = new ClassPathXmlApplicationContext(CONFIG);
		} catch (RuntimeException e) {
			e.printStackTrace();
			System.out.println("spring 初始化错误");
		}

		return context;
	}

	// 使用泛型编程
	public static <T> T getBean(String beanName, Class<T> requiredType) {
		T object = null;
		if (getApplicationContext() != null) {
			object = getApplicationContext().getBean(beanName, requiredType);
		}
		
		return object;
	}

}
